import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateCreateDto.dart';
import 'package:bxultimate/widgets/app/app_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart' hide TDInput, TDInputType;

class RadioForm extends StatefulWidget {
  final Function(OrganizationAnnouncementTemplateCreateDto) addFromItem;

  final OrganizationAnnouncementTemplateCreateDto dto;

  const RadioForm({super.key, required this.addFromItem, required this.dto});

  @override
  State<RadioForm> createState() => _RadioFormState();
}

class _RadioFormState extends State<RadioForm> {
  TextEditingController placeholderController = TextEditingController();
  TextEditingController placeholderTwoController = TextEditingController();
  TextEditingController radioTextController = TextEditingController();

  var requiredSelected = false;
  List<RadioList> radioList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    radioList = widget.dto.attributes?.radioList ?? [];
    placeholderController.text = widget.dto.placeholder ?? '';
    placeholderTwoController.text = widget.dto.placeholderTwo ?? '';
    requiredSelected = widget.dto.required ?? false;
  }

  var characters = ['A', 'B', 'C', 'D', 'E'];

  saveRadio() {
    var name = characters[radioList.length];
    radioList.add(RadioList(
        name: name,
        value: radioTextController.text,
        icon: 'tn-icon-reduce-circle-fill'));
    setState(() {});
    Get.back();
  }

  removeRadio(e) {
    radioList.remove(e);
    setState(() {});
  }

  bool requiredChange(bool value) {
    setState(() {
      requiredSelected = value;
    });
    return value;
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        TDInput(
          controller: placeholderController,
          leftLabel: '标题',
          required: true,
          type: TDInputType.special,
          hintText: '请输入标题',
          textAlign: TextAlign.end,
          backgroundColor: Colors.transparent,
        ),
        TDInput(
          controller: placeholderTwoController,
          leftLabel: '提示文字',
          hintText: '请输入提示文字',
          type: TDInputType.special,
          textAlign: TextAlign.end,
          backgroundColor: Colors.transparent,
        ),
        TDButton(
          text: '添加选项',
          size: TDButtonSize.small,
          theme: TDButtonTheme.light,
          isBlock: true,
          onTap: () {
            if(radioList.length == 5){
              SmartDialog.showNotify(msg: '最多添加5个选项', notifyType: NotifyType.failure);
              return;
            }
            showGeneralDialog(
              context: context,
              pageBuilder: (BuildContext buildContext,
                  Animation<double> animation,
                  Animation<double> secondaryAnimation) {
                return TDInputDialog(
                  textEditingController: radioTextController,
                  title: '请输入选项名称',
                  rightBtn:
                      TDDialogButtonOptions(title: '确定', action: saveRadio),
                );
              },
            );
          },
        ),
        Column(
          children: [
            ...radioList.map((e) => TDCell(
                  title: '${e.name}',
                  description: '${e.value??''}',
                  rightIconWidget: InkWell(
                    onTap: () {
                      removeRadio(e);
                    },
                    child: Icon(Icons.close),
                  ),
                ))
          ],
        ),
        TDCell(
          title: '必填',
          rightIconWidget:
              TDSwitch(isOn: requiredSelected, onChanged: requiredChange),
        ),
        Gaps.vGap12,
        TDButton(
          text: '确认',
          theme: TDButtonTheme.primary,
          isBlock: true,
          onTap: () {
            OrganizationAnnouncementTemplateCreateDto dto =
                OrganizationAnnouncementTemplateCreateDto();
            Attributes attributes = Attributes();
            attributes = attributes.copyWith(radioList: radioList);
            dto = dto.copyWith(
                placeholder: placeholderController.text,
                placeholderTwo: placeholderTwoController.text,
                textInput: placeholderController.text,
                textinputplaceholder: placeholderTwoController.text,
                required: requiredSelected,
                type: 4,
                attributes: attributes);
            widget.addFromItem(dto);
          },
        )
      ],
    );
  }
}
